<?php
namespace think;

require __DIR__ . '/../vendor/autoload.php';

use think\facade\Db; // 导入Db类
use think\facade\Env;
(new App())->console;

$version = 1.22;

$update_version = 0;
if (is_file('update.lock')) {
    (float)$update_version = file_get_contents('update.lock');
    if ($version<=$update_version) {
        exit('请勿重复更新');
    }
}
//V1.1
if(1.1>$update_version)
{
    $tableName = env('database.prefix', 'lnk_').'config';
    $sql = "
    UPDATE ".$tableName." SET type = REPLACE(type, 'lnksns', 'lnk');
    ";
    Db::execute($sql);
    
    $tableName = env('database.prefix', 'lnk_').'permissions';
    $sql = "
    UPDATE ".$tableName." SET route = REPLACE(route, 'lnksns', 'lnk'),component = REPLACE(component, 'lnksns', 'lnk');
    ";
    Db::execute($sql);
}

//V1.2
if(1.2>$update_version)
{
    $tableName = env('database.prefix', 'lnk_').'sns_user_bind';
    if (!Db::query("SHOW TABLES LIKE '$tableName'")) {
        $sql = "CREATE TABLE `".$tableName."` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `user_id` bigint(20) DEFAULT NULL COMMENT '用户ID',
            `type` tinyint(1) NOT NULL DEFAULT '0' COMMENT '类型',
            `bind` varchar(60) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '绑定识别码',
            `wx_unionid` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '微信unionid',
            `create_time` int(11) DEFAULT NULL COMMENT '创建时间',
            `update_time` int(11) DEFAULT NULL COMMENT '更新时间',
            PRIMARY KEY (`id`) USING BTREE,
            KEY `_id` (`id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户绑定表';
        ";
        Db::execute($sql);


        $tableName = env('database.prefix', 'lnk_').'sns_user';
        $db_table_info = Db::query("desc `".$tableName."`");
        $finds = [];
        foreach($db_table_info as $v){
            $finds[] = $v['Field'];
        }

        if(!in_array('type',$finds))
        {
            $sql = "
            ALTER TABLE `".$tableName."` ADD `type` tinyint(1) DEFAULT '0' COMMENT '类型' AFTER `status`;
            ";
            Db::execute($sql);
            $sql = "
            UPDATE ".$tableName." SET type = 1 WHERE weixin_openid<>'';
            ";
            Db::execute($sql);

            $new_tableName = env('database.prefix', 'lnk_').'sns_user_bind';
            $sql = "
            INSERT INTO `".$new_tableName."` (`user_id`, `bind`, `type`) SELECT `id`, `weixin_openid`, 1 FROM `".$tableName."` WHERE `weixin_openid`<>'';
            ";
            Db::execute($sql);

        }

    }


}
//V1.21
if(1.21>$update_version)
{

    $tableName = env('database.prefix', 'lnk_').'sns_dynamic';
    $db_table_info = Db::query("desc `".$tableName."`");
    $finds = [];
    foreach($db_table_info as $v){
        $finds[] = $v['Field'];
    }

    if(!in_array('mold',$finds))
    {
        $sql = "
        ALTER TABLE `".$tableName."` ADD `mold` tinyint(3) NOT NULL DEFAULT '1' COMMENT '模型' AFTER `type`;
        ";
        Db::execute($sql);
    }
}
//V1.22
if(1.22>$update_version)
{
    $tableName = env('database.prefix', 'lnk_').'admin';
    $sql = "
    ALTER TABLE `".$tableName."` CHANGE `login_ip` `login_ip` VARCHAR(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '登录 ip';
    ";
    Db::execute($sql);
    $tableName = env('database.prefix', 'lnk_').'sns_user_bind';
    $sql = "
    ALTER TABLE `".$tableName."` CHANGE `user_id` `user_id` BIGINT(20) NULL COMMENT '用户ID';
    ";
    Db::execute($sql);
    
}

file_put_contents('update.lock',$version);
echo '更新成功，请及时删除当前文件';